package log

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"net"
	"net/http"
	"net/http/httputil"
	"os"
	"runtime/debug"
	"strings"
	"time"
)

type LogConfig struct {
	Level      string `mapstructure:"level" json:"level"`
	Filename   string `mapstructure:"filename" json:"filename"`
	MaxSize    int    `mapstructure:"max_size" json:"max_size"`
	MaxAge     int    `mapstructure:"max_age" json:"max_age"`
	MaxBackups int    `mapstructure:"max_backups" json:"max_backups"`
}

func Init(mode string) (err error) {
	var logger = &LogConfig{
		Filename:   "./log/logInfo",
		MaxSize:    200,
		MaxAge:     30,
		MaxBackups: 7,
	}
	writeSyncer := getLogWriter(
		logger.Filename,
		logger.MaxSize,
		logger.MaxBackups,
		logger.MaxAge,
	)
	var core zapcore.Core
	encoder := getEncoder()
	var lv = new(zapcore.Level)
	err = lv.UnmarshalText([]byte("debug"))
	if err != nil {
		return err
	}
	if mode == "dev" {
		consoleEncoder := zapcore.NewConsoleEncoder(getConsoleEncoderConfig())
		core = zapcore.NewTee(
			zapcore.NewCore(encoder, writeSyncer, lv),
			zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), zap.DebugLevel))
	} else {
		core = zapcore.NewCore(encoder, writeSyncer, lv)
	}
	//zap.AddCallerSkip(1)   caller 层数
	lg := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
	zap.ReplaceGlobals(lg)
	return
}
func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.TimeKey = "time"
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
	encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
	return zapcore.NewJSONEncoder(encoderConfig)
}
func getConsoleEncoderConfig() zapcore.EncoderConfig {
	return zapcore.EncoderConfig{
		TimeKey:        "ts",
		LevelKey:       "level",
		NameKey:        "logger",
		CallerKey:      "caller",
		FunctionKey:    zapcore.OmitKey,
		MessageKey:     "msg",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseLevelEncoder,
		EncodeTime:     zapcore.ISO8601TimeEncoder,
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder,
	}
}
func getLogWriter(fn string, maxSize, maxBackup, maxAge int) zapcore.WriteSyncer {
	lumberjackLogger := &lumberjack.Logger{
		Filename:   fn,
		MaxSize:    maxSize,
		MaxBackups: maxBackup,
		MaxAge:     maxAge,
	}
	return zapcore.AddSync(lumberjackLogger)
}

// GinLogger 接收gin框架默认的日志
func GinLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		path := c.Request.URL.Path
		query := c.Request.URL.RawQuery
		c.Next()

		cost := time.Since(start)
		zap.L().Info(path,
			zap.Int("status", c.Writer.Status()),
			zap.String("method", c.Request.Method),
			zap.String("path", path),
			zap.String("query", query),
			zap.String("ip", c.ClientIP()),
			zap.String("user-agent", c.Request.UserAgent()),
			zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
			zap.Duration("cost", cost),
		)
	}
}

// GinRecovery recover掉项目可能出现的panic，并使用zap记录相关日志
func GinRecovery(stack bool) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				// Check for a broken connection, as it is not really a
				// condition that warrants a panic stack trace.
				var brokenPipe bool
				if ne, ok := err.(*net.OpError); ok {
					if se, ok := ne.Err.(*os.SyscallError); ok {
						if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
							brokenPipe = true
						}
					}
				}

				httpRequest, _ := httputil.DumpRequest(c.Request, false)
				if brokenPipe {
					zap.L().Error(c.Request.URL.Path,
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
					)
					// If the connection is dead, we can't write a status to it.
					c.Error(err.(error)) // nolint: errcheck
					c.Abort()
					return
				}

				if stack {
					zap.L().Error("[Recovery from panic]",
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
						zap.String("stack", string(debug.Stack())),
					)
				} else {
					zap.L().Error("[Recovery from panic]",
						zap.Any("error", err),
						zap.String("request", string(httpRequest)),
					)
				}
				c.AbortWithStatus(http.StatusInternalServerError)
			}
		}()
		c.Next()
	}
}

func Info(msg string, value interface{}) {
	zap.L().Info(msg, zap.Any(msg, value))
}
func Infof(format string, value ...interface{}) {
	zap.L().Info("", zap.Any("info", fmt.Sprintf(format, value...)))
}
func Debug(msg string, value interface{}) {
	zap.L().Debug(msg, zap.Any(msg, value))
}
func Debugf(format string, value ...interface{}) {
	zap.L().Info("", zap.Any("debug", fmt.Sprintf(format, value...)))
}
func Warn(msg string, value interface{}) {
	zap.L().Warn(msg, zap.Any(msg, value))
}
func Warnf(format string, value ...interface{}) {
	zap.L().Info("warn", zap.Any("warn", fmt.Sprintf(format, value...)))
}
func Error(msg string, value interface{}) {
	zap.L().Error(msg, zap.Any(msg, value))
}
func Errorf(format string, value ...interface{}) {
	zap.L().Info("error", zap.Any("error", fmt.Sprintf(format, value...)))
}
func Fatal(msg string, value interface{}) {
	zap.L().Fatal(msg, zap.Any(msg, value))
}
func Fatalf(format string, value ...interface{}) {
	zap.L().Info("warn", zap.Any("fatal", fmt.Sprintf(format, value...)))
}
